package com.ylr.base.common.exception;

import com.ylr.base.common.enums.ResultEnum;
import com.ylr.base.common.interfaces.BaseResultEnum;
import com.ylr.base.common.tool.Result;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2023-04-19 22:33:10
 * className YlrException 自定义运行时异常处理类
 * version: 1.0
 * description:
 * 		继承自RuntimeException类
 */
public class YlrException extends RuntimeException {
	private static final long serialVersionUID = 1L;

	/**
	 * 异常状态码
	 */
	private Integer code;

	public YlrException() {
		this(ResultEnum.SERVICE_FAIL);
	}

	public YlrException(Integer code, String message) {
		super(message);
		this.code = code;
	}

	/**
	 * 异常消息存储于上级对象中
	 * @param msg 异常消息
	 */
	public YlrException(String msg) {
		super(msg);
		this.code = ResultEnum.SERVICE_FAIL.getCode();
	}

	public YlrException(String msg, Throwable e) {
		super(msg, e);
		this.code = ResultEnum.SERVICE_FAIL.getCode();
	}

	public <T> YlrException(Result<T> result) {
		super(result.getMsg());
		this.code = result.getCode();
	}

	public YlrException(BaseResultEnum resultEnum) {
		super(resultEnum.getMsg());
		this.code = resultEnum.getCode();
	}

	public YlrException(BaseResultEnum resultEnum, Throwable e) {
		super(resultEnum.getMsg(), e);
		this.code = resultEnum.getCode();
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

}
